package com.person.tag;
 
import javax.servlet.jsp.JspException;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject; 
import org.springframework.util.StringUtils;

import com.person.common.InitApplication;
import com.person.ntv.shiro.util.UrlFilter;

public class LimitTag extends BaseTag {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 对应SysResource的resCode
	 */
	private String value;

	public String getValue() {
		return value;
	}
    
	public void setValue(String value) {
		this.value = value;
	}

	public void release() {
		super.release();
	}

	@Override
	public int doEndTag() throws JspException {
		 StringBuffer out = new StringBuffer("");
		 Subject subject = SecurityUtils.getSubject();
		 //是否有权限
		 boolean hasPrivage=false;
		 UrlFilter urlFilter=InitApplication.urlMap.get(this.value);
		 if(urlFilter==null||StringUtils.isEmpty(urlFilter.getRoles())) hasPrivage=true;
		 for(String role:urlFilter.getRoles().split(",")){
		     if(subject.hasRole(role)){
		    	 hasPrivage=true;
		    	 break;
		     }
		 }
		 if(hasPrivage&&this.bodyContent!=null){
			  out.append(this.bodyContent.getString());
		  } 
		 TagUtils.write(pageContext, out.toString());
	    return EVAL_BODY_BUFFERED;
	}

	@Override
	public int doStartTag() throws JspException {
		 return EVAL_PAGE;
	}
}
